package com.qf.service.impl;/*
 *徐韬
 *2021/10/26
 *18:56
 */

import com.qf.constons.resultCode;
import com.qf.dto.UserDto;
import com.qf.exceptions.queryDataException;
import com.qf.mapper.UserMapper;
import com.qf.pojo.TAdminUser;
import com.qf.pojo.checkType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.QueryTimeoutException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

@Service
@Transactional
public class TAdminUserService implements com.qf.service.TAdminUserService {
    UserMapper userMapper;
        @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public TAdminUser login(UserDto userDto) {
                //判断前端传过来的数据是不是空的
            if(userDto.isNull(checkType.ADD)){
                throw new queryDataException("用户名或密码错误", resultCode.USERNAME_OR_PASSWORD);
            }
            TAdminUser user = userMapper.selectByUserCode(userDto.getUsercode());
            if(user==null){//如果传过来数据 对数据库进行查询 如果没查到，用户名错误
                throw new queryDataException("用户名或密码错误", resultCode.USERNAME_OR_PASSWORD);
            }
            //将传递过来的普通密码 转换为md5密码 在与数据库中MD5密码进行比较
            String password = userDto.getPassword();
            String passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        String password1 = user.getPassword();
        if(!passwordMd5.equalsIgnoreCase(password1)){
            throw new queryDataException("用户名或密码错误", resultCode.USERNAME_OR_PASSWORD);
        }else{
            System.out.println("登陆成功");
        }
        return user;

    }
}
